<template>

  <el-form :model="form" label-width="120px" v-loading="loading">

    <el-form-item label="图片">
      <el-upload
          class="avatar-uploader"
          action="/api/common/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
      >
        <img v-if="form.pic" class="avatar" :src="'/api/common/download?name='+form.pic" alt=""/>
        <el-icon v-else>
          <Plus/>
        </el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item label="评分">
      <el-rate
          v-model="form.score"
          disabled
          show-score
          text-color="#ff9900"
          score-template="{value} 分"
      />
    </el-form-item>
    <el-form-item label="销量">
      <el-input v-model="form.sales" disabled/>
    </el-form-item>
    <el-form-item label="余额">
      <el-input v-model="form.balance" disabled/>
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="form.name"/>
    </el-form-item>
    <el-form-item label="介绍">
      <el-input v-model="form.introduce" type="textarea"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">提交</el-button>
    </el-form-item>
  </el-form>
</template>


<script lang="ts" setup>


import {onMounted, reactive, ref, watch} from "vue";
import {getBusinessByIdApi, updateApi} from "@/api/Info";

import {ElMessage, UploadProps} from "element-plus";

import {getToken} from "@/utils/auth";

import {Plus} from '@element-plus/icons-vue'

import type {UploadFile} from 'element-plus'

const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const disabled = ref(false)
const loading = ref(true)


// do not use same name with ref
const form = reactive({
  balance: 0,
  distance: 0,
  id: 0,
  introduce: "",
  name: "",
  password: "",
  pic: "",
  sales: "",
  score: 0,
  username: ""
})

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
  form.pic = URL.createObjectURL(uploadFile.raw!)
  console.log(response)
  if (response.code == 200) {
    form.pic = response.data
  }
}
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('图片必须是JPG格式！')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片大小不能超过2MB！')
    return false
  }
  return true
}
const handleRemove = (file: UploadFile) => {
  console.log(file)
}


onMounted(() => {
  getTableData()
})

function getTableData() {
  getBusinessByIdApi(getToken()).then((response: any) => {
    if (response.code === 200) {
      Object.assign(form, response.data);
      form.score = parseFloat(response.data.score);
    } else {
      ElMessage.warning(response.msg)
    }
    loading.value = false
  }).catch(function (res) {
    loading.value = false
    ElMessage.error("页面加载失败")
  })

}

function onSubmit() {
  updateApi(form).then((response: any) => {
    if (response.code === 200) {
      ElMessage.success(response.msg)
    } else {
      ElMessage.warning(response.msg)
    }
    loading.value = false
  }).catch(function (res) {
    loading.value = false
    ElMessage.error("页面加载失败")
  })

}

</script>


